//
//  ZJReportDetailViewController.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/6/14.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJReportDetailViewController.h"
#import "ZJRBButtonsView.h"
#import "ZJRBTextFliedView.h"
#import "ZJNeedAndSpecialityModel.h"
#import "XLSlideMenu.h"
#import "ZJTabBarViewController.h"
#import "LPLeftViewController.h"
#import "ZJLoginJudgeManager.h"
#import "LoginViewController.h"
@interface ZJReportDetailViewController ()<ZJRBButtonsViewDelegate>
{
    NSArray *_needArray;/**< 需求数组 */
    NSArray *_specialityArray;/**< 特长数组 */
    BOOL _isCheck;/**< 是否是党员审核中 */
}
/** scrollView */
@property (nonatomic, strong) UIScrollView *scrollView;
/** 姓名 */
@property (nonatomic, strong) ZJRBTextFliedView *name;
/** 单位 */
@property (nonatomic, strong) ZJRBTextFliedView *workAddress;
/** 其他特长 */
@property (nonatomic, strong) ZJRBTextFliedView *otherSpeciality;
/** 特长 */
@property (nonatomic, strong) ZJRBButtonsView *speciality;
/** 需求 */
@property (nonatomic, strong) ZJRBButtonsView *need;
/** 其他需求 */
@property (nonatomic, strong) ZJRBTextFliedView *otherNeed;
/** 完成 */
@property (nonatomic, strong) UIButton *complete;

@end

@implementation ZJReportDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = @"党员报到";
    if (self.titleString.length != 0) {
        self.titlelabel.text = self.titleString;
    }
    _isCheck = NO;
    [self setupUI];
//    [self isCheck];
    [self netWorking];

}


- (void)setupUI{
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.name];
    [self.scrollView addSubview:self.workAddress];
    [self.scrollView addSubview:self.speciality];
    [self.scrollView addSubview:self.otherSpeciality];
    [self.scrollView addSubview:self.need];
    [self.scrollView addSubview:self.otherNeed];
    [self.scrollView addSubview:self.complete];
    
    self.name.sd_layout
    .topSpaceToView(self.scrollView, 5 * [UIScreen zj_screenHScale])
    .leftSpaceToView(self.scrollView, 0)
    .rightSpaceToView(self.scrollView, 0)
    .heightIs(self.name.height);
    
    self.workAddress.sd_layout
    .topSpaceToView(self.name, 0)
    .leftEqualToView(self.name)
    .rightEqualToView(self.name)
    .heightIs(self.workAddress.height);
    
    self.speciality.sd_layout
    .topSpaceToView(self.workAddress, 0)
    .leftEqualToView(self.name)
    .rightEqualToView(self.name)
    .heightIs(self.speciality.height);
    
    self.otherSpeciality.sd_layout
    .topSpaceToView(self.speciality, 0)
    .leftEqualToView(self.name)
    .rightEqualToView(self.name)
    .heightIs(self.otherSpeciality.height);
    
    self.need.sd_layout
    .topSpaceToView(self.otherSpeciality, 0)
    .leftEqualToView(self.name)
    .rightEqualToView(self.name)
    .heightIs(self.need.height);
    
    self.otherNeed.sd_layout
    .topSpaceToView(self.need, 0)
    .leftEqualToView(self.name)
    .rightEqualToView(self.name)
    .heightIs(self.otherNeed.height);
    
    self.complete.sd_layout
    .topSpaceToView(self.otherNeed, 20 * [UIScreen zj_screenHScale])
    .leftSpaceToView(self.scrollView, 27 * [UIScreen zj_screenWScale])
    .rightSpaceToView(self.scrollView, 27 * [UIScreen zj_screenWScale])
    .heightIs(44 * [UIScreen zj_screenHScale]);
    
    [self.scrollView setupAutoContentSizeWithBottomView:self.complete bottomMargin:30 * [UIScreen zj_screenHScale]];
}
#pragma mark - 判断是否党员报到审核中
- (void)isCheck{
    if ([[ZJUserInfoModel sharedUserInfo].USER_MODEL_JOB.AUTH_STATUS isEqualToString:@"1"]) {
        [self.complete setTitle:@"正在审核中..." forState:UIControlStateNormal];
        self.complete.backgroundColor = [UIColor zj_colorWithHexString:@"bdbdbd"];
        [self.complete setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.complete.userInteractionEnabled = NO;
        self.name.userInteractionEnabled = NO;
        self.workAddress.userInteractionEnabled = NO;
        self.otherSpeciality.userInteractionEnabled = NO;
        self.speciality.userInteractionEnabled = NO;
        self.need.userInteractionEnabled = NO;
        self.otherNeed.userInteractionEnabled = NO;
        
        self.name.textField.backgroundColor = [UIColor themeGrayColor];
        self.workAddress.textField.backgroundColor = [UIColor themeGrayColor];
        self.otherNeed.textField.backgroundColor = [UIColor themeGrayColor];
        self.otherSpeciality.textField.backgroundColor =[UIColor themeGrayColor];
        _isCheck = YES;
        if (self.name.textField.text.length == 0) {
            self.name.textField.text = @"";
        }
        if (self.workAddress.textField.text.length == 0) {
            self.workAddress.textField.text = @"";
        }
    }
}

#pragma mark - 网络请求
//获取特长、需求信息
- (void)netWorking{
    [[LPShowNoDataManager showNoDataManager] removeEmptyView];
    __weak typeof(self) weakSelf = self;
    [NetWorkingManager postWithUrlString:[NetWorkingURL listSpecialtys] Parameter:nil Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            _needArray = [NSArray yy_modelArrayWithClass:[ZJNeedAndSpecialityModel class] json:[object objectForKey:@"NEEDS"]];
            _specialityArray = [NSArray yy_modelArrayWithClass:[ZJNeedAndSpecialityModel class] json:[object objectForKey:@"SPECIALTYS"]];
            [weakSelf setNeedAndSpeciality_UpdateUI];
        }
    } Fail:^(id object) {
        [[LPShowNoDataManager showNoDataManager] showEmptyViewWithErrorView:weakSelf.view];
        [LPShowNoDataManager showNoDataManager].noContentViewTapedBlock = ^{
            [weakSelf netWorking];
        };

    }];
}

- (void)setNeedAndSpeciality_UpdateUI{
    NSArray *needSeleArray = [[ZJUserInfoModel sharedUserInfo].USER_MODEL_JOB.NEED_DSCODE_VALUE componentsSeparatedByString:@","];
    NSMutableArray *needNameString = [[[ZJUserInfoModel sharedUserInfo].USER_MODEL_JOB.NEED_DSCODE_NAMES componentsSeparatedByString:@","] mutableCopy];
    NSArray *specialitySeleArray = [[ZJUserInfoModel sharedUserInfo].USER_MODEL_JOB.SPECIALTY_DSCODE_VALUE componentsSeparatedByString:@","];
    NSMutableArray *specialityNameString = [[[ZJUserInfoModel sharedUserInfo].USER_MODEL_JOB.SPECIALTY_DSCODE_NAMES componentsSeparatedByString:@","] mutableCopy];
    
    [needNameString removeObjectsInRange:NSMakeRange(0, needSeleArray.count)];
    [specialityNameString removeObjectsInRange:NSMakeRange(0, specialitySeleArray.count)];
    //把数组转换成String
    self.otherNeed.textField.text = [needNameString componentsJoinedByString:@","];
    self.otherSpeciality.textField.text = [specialityNameString componentsJoinedByString:@","];
    [self.need updateUI_dataArray:_needArray seleArray:needSeleArray isCheck:_isCheck];
    [self.speciality updateUI_dataArray:_specialityArray seleArray:specialitySeleArray isCheck:_isCheck];
    if (_isCheck) {//党员报到审核中
        if (self.otherNeed.textField.text.length == 0) {
            self.otherNeed.textField.text = @" ";
        }
        if (self.otherSpeciality.textField.text.length == 0) {
            self.otherSpeciality.textField.text = @" ";
        }
    }
}



#pragma mark - ZJRBButtonsViewDelegate(buttons的Frame改变) 协议方法
- (void)ZJRBButtonsViewDelegate_View:(ZJRBButtonsView *)view{
    if (view == self.need) {
        self.need.sd_layout
        .topSpaceToView(self.otherSpeciality, 0)
        .leftEqualToView(self.name)
        .rightEqualToView(self.name)
        .heightIs(self.need.height);
        [self.need updateLayout];
    }else{
        self.speciality.sd_layout
        .topSpaceToView(self.workAddress, 0)
        .leftEqualToView(self.name)
        .rightEqualToView(self.name)
        .heightIs(self.speciality.height);
        [self.speciality updateLayout];
    }
    
}
#pragma mark - 点击事件
///完成提交
- (void)action_complete{
    if (self.name.textField.text.length == 0) {
        [YJProgressHUD showMessage:@"姓名不能为空" inView:self.view];
        [self.name.textField becomeFirstResponder];
        return;
    }
    if (self.workAddress.textField.text.length == 0) {
        [YJProgressHUD showMessage:@"单位不能为空" inView:self.view];
        [self.workAddress.textField becomeFirstResponder];
        return;
    }
    
    [YJProgressHUD showMessage:@"提交中..." duration:10 view:self.view];
    [self netWorking_complete];
}

- (void)netWorking_complete{
    NSMutableString *NEED_DSCODE_VALUE = [NSMutableString string];//需求代码值(逗号分隔)
    NSMutableString *NEED_DSCODE_NAMES = [NSMutableString string];//需求名称（逗号分隔）
    NSMutableString *SPECIALTY_DSCODE_VALUE = [NSMutableString string];//需求代码值(逗号分隔)
    NSMutableString *SPECIALTY_DSCODE_NAMES = [NSMutableString string];//需求名称（逗号分隔）
    
    for (int i = 0; i <= self.need.seletArray.count; i ++) {
        if (i == self.need.seletArray.count) {
            if ( self.otherNeed.textField.text.length > 0) {
                [NEED_DSCODE_NAMES appendString:[NSString stringWithFormat:@",%@",self.otherNeed.textField.text]];
            }
        }else{
            ZJNeedAndSpecialityModel *model = self.need.seletArray[i];
            if (i == 0) {
                [NEED_DSCODE_VALUE appendString:model.CODE_VALUE];
                [NEED_DSCODE_NAMES appendString:model.CODE_NAME];
            }else {
                [NEED_DSCODE_VALUE appendString:[NSString stringWithFormat:@",%@",model.CODE_VALUE]];
                [NEED_DSCODE_NAMES appendString:[NSString stringWithFormat:@",%@",model.CODE_NAME]];
            }
        }
    }
    for (int i = 0; i <= self.speciality.seletArray.count; i ++) {
        if (i == self.speciality.seletArray.count ) {
            if (self.otherSpeciality.textField.text.length > 0) {
                [SPECIALTY_DSCODE_NAMES appendString:[NSString stringWithFormat:@",%@",self.otherSpeciality.textField.text]];
            }
        }else{
            ZJNeedAndSpecialityModel *model = self.speciality.seletArray[i];
            if (i == 0) {
                [SPECIALTY_DSCODE_VALUE appendString:model.CODE_VALUE];
                [SPECIALTY_DSCODE_NAMES appendString:model.CODE_NAME];
            }else{
                [SPECIALTY_DSCODE_VALUE appendString:[NSString stringWithFormat:@",%@",model.CODE_VALUE]];
                [SPECIALTY_DSCODE_NAMES appendString:[NSString stringWithFormat:@",%@",model.CODE_NAME]];
            }
        }
    }
    NSString *USERID = @"";
    NSString *ORG_ACCOUNT = @"";
    if (self.registerModel.USER_ID) {//注册模块
        USERID = [NSString isNilStringReturnString:self.registerModel.USER_ID];
        ORG_ACCOUNT = self.registerModel.ACCOUNT_ID;
    }else{
        USERID = [NSString isNilStringReturnString:[ZJUserInfoModel sharedUserInfo].USER_MODEL_JOB.ID];
        ORG_ACCOUNT = [ZJUserInfoModel sharedUserInfo].ORG_ACCOUNT.ID;
    }
    
    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{@"USERID":USERID,
                           @"NEED_DSCODE_GROUP":@"需求",
                           @"NEED_DSCODE_VALUE":NEED_DSCODE_VALUE,
                           @"NEED_DSCODE_NAMES":NEED_DSCODE_NAMES,
                           @"SPECIALTY_DSCODE_GROUP":@"特长",
                           @"SPECIALTY_DSCODE_VALUE":SPECIALTY_DSCODE_VALUE,
                           @"SPECIALTY_DSCODE_NAMES":SPECIALTY_DSCODE_NAMES,
                           @"COMP_NAME":self.workAddress.textField.text,
                           @"DEPT_ID":self.model.ID ,
                           @"ACCOUNTID":ORG_ACCOUNT,
                           @"USER_NAME":self.name.textField.text};
//    __weak typeof(self) weakSelf = self;
    [NetWorkingManager postWithUrlString:[NetWorkingURL registUpdataUserNeedAndSpecialty] Parameter:dict Success:^(id object) {
        [YJProgressHUD hide];
        if ([object objectForKey:@"ORG_ACCOUNT"]) {
//            [MobClick event:[UMMStatisticsString partyMemberRegister]];
            [YJProgressHUD showMsgWithoutView:@"提交成功"];
            [[NSUserDefaults standardUserDefaults] setObject:[object yy_modelToJSONString] forKey:USER_INFO];
            [ZJUserInfoModel updateUserInfoFormNSUserDefaults];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if (weakSelf.registerModel.USER_ID) {//注册模块
                [weakSelf loginJudge];
            }else{
                [[NSNotificationCenter defaultCenter] postNotificationName:@"changeUserInfo" object:nil];
            }
                [weakSelf backViewControllerType];
                
            });
        }else{
            [YJProgressHUD showMsgWithoutView:@"提交失败"];
        }
    } Fail:^(id object) {
        [YJProgressHUD showMsgWithoutView:@"提交失败"];
    }];
}
//退回方式
- (void)backViewControllerType{
    
    if (self.isModel) {
        [LPCostomPressentAnimationSimilarityPushAnimation backLastVc];
        [self dismissViewControllerAnimated:NO completion:nil];
    }else{
        [self.navigationController popToRootViewControllerAnimated:YES];
   
    }
}

- (void)loginJudge{
    NSString *string =  [ZJLoginJudgeManager loginJudge];
    if ([string isEqualToString:@"pass"]) {
        [self loginSuccess];
    }else if ([string isEqualToString:@"check"]){//跳党员报到
        
    }else if([string isEqualToString:@"checkTwo"]){//需要 中间页面（组织报到、党员报到）
        
    }else if([string isEqualToString:@"login"]){//跳登录页面
        [YJProgressHUD showMessage:@"请重新登录" inView:[UIApplication sharedApplication].keyWindow];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[LoginViewController new]];
            [UIApplication sharedApplication].keyWindow.rootViewController = nav;
        });
    }else{
        [YJProgressHUD showMessage:string inView:self.view];
        __weak typeof(self) weakSelf = self;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           [weakSelf backViewControllerType];
        });
    }
}



- (void)loginSuccess{
  
    [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"isLogin"];
    ZJTabBarViewController *zj = [[ZJTabBarViewController alloc] init];
    //左侧菜单
    LPLeftViewController *rightVC = [[LPLeftViewController alloc] init];
    //创建滑动菜单
    XLSlideMenu *slideMenu = [[XLSlideMenu alloc] initWithRootViewController:zj];
    //设置左右菜单
    slideMenu.rightViewController = rightVC;
    [UIApplication sharedApplication].keyWindow.rootViewController =slideMenu;
}



#pragma mark - Setter


#pragma mark - Getters
- (UIScrollView *)scrollView{
    if (_scrollView == nil){
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, navHeight, self.view.width, self.view.height - navHeight)];
        
    }
    return _scrollView;
}

- (ZJRBTextFliedView *)name{
    if (_name == nil){
        _name = [[ZJRBTextFliedView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 90 * [UIScreen zj_screenHScale]) title:@"姓名 :"];
        _name.textField.placeholder = @"请输入姓名...";
        _name.maxNumber = 50;
        _name.textField.text = [ZJUserInfoModel sharedUserInfo].ORG_ACCOUNT.USER_NAME;
        if (self.registerModel) {
            _name.textField.text = self.registerModel.PARTYMEMBER_NAME;
        }
    }
    return _name;
}
- (ZJRBTextFliedView *)workAddress{
    if (_workAddress == nil){
        _workAddress = [[ZJRBTextFliedView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.name.height) title:@"单位 :"];
        _workAddress.textField.placeholder = @"请输入单位名称...";
        _workAddress.textField.text = [ZJUserInfoModel sharedUserInfo].USER_MODEL_JOB.COMP_NAME;
        _workAddress.maxNumber = 50;
    }
    return _workAddress;
}
- (ZJRBTextFliedView *)otherSpeciality{
    if (_otherSpeciality == nil){
        _otherSpeciality = [[ZJRBTextFliedView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.name.height) title:@"其他 :"];
        _otherSpeciality.maxNumber = 50;
        _otherSpeciality.textField.placeholder = @"请输入其他特长...";
    }
    return _otherSpeciality;
}
- (ZJRBTextFliedView *)otherNeed{
    if (_otherNeed == nil){
        _otherNeed = [[ZJRBTextFliedView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.name.height) title:@"其他 :"];
        _otherNeed.textField.placeholder = @"请输入其他需求...";
        _otherNeed.maxNumber = 50;
    }
    return _otherNeed;
}

- (ZJRBButtonsView *)speciality{
    if (_speciality == nil){
        _speciality = [[ZJRBButtonsView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 40 * [UIScreen zj_screenHScale]) title:@"特长 :"];
        _speciality.delegate = self;
    }
    return _speciality;
}
- (ZJRBButtonsView *)need{
    if (_need == nil){
        _need = [[ZJRBButtonsView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 40 * [UIScreen zj_screenHScale]) title:@"需求 :"];
        _need.delegate = self;
    }
    return _need;
}

- (UIButton *)complete{
    if (_complete == nil){
        _complete = [UIButton new];
        [_complete setTitle:@"完成" forState:UIControlStateNormal];
        [_complete setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _complete.backgroundColor = [UIColor themeColor];
        _complete.titleLabel.font = [UIFont titleTextFont];
        _complete.layer.cornerRadius = 4 * [UIScreen zj_screenHScale];
        [_complete addTarget:self action:@selector(action_complete) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _complete;
}

@end
